AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

include("shared.lua")

ENT.ID = ""
ENT.Position = Vector(0,0,0)
ENT.Target	= nil
ENT.FireDelay = 0
ENT.NextAttack = CurTime() + ENT.FireDelay		
ENT.Reference = nil
ENT.TType = "atf"
ENT.Level = 1
ENT.TurretOwner = nil

function ENT:Initialize()
	
	--self:SetModel( "models/buildables/sentry1.mdl" )
	self:SetModel( "models/Combine_turrets/Floor_turret.mdl" )
		
	local allTurrets = ents.FindByClass("adv_turret_fire")
	self.ID = self.TType.."_"..tostring(#allTurrets)
	
	-- initial turret set up
		self.Reference = TURRETS.AllTurrets[self.TType..self.Level]
		self.Damage = self.Reference.Damage
		self.Range = self.Reference.Range
		self.Strength = self.Reference.Strength
		self.FireDelay = self.Reference.FireDelay
		self.BurnTime = self.Reference.BurnTime
		
	self:PhysicsInit( SOLID_VPHYSICS )
	self.Position = self:GetPos()
end


function ENT:Owner(pl)
	self.TurretOwner = pl
end


function ENT:AttackTarget()
	if CurTime() >= self.NextAttack then
		local ang = self:GetAngles()
		--local pos = self:GetBonePosition(self:LookupBone("turret_muzzle")) -- for sentry
		local pos = self:GetAttachment(self:LookupAttachment("light")).Pos
		local ed = EffectData()
		ed:SetOrigin(pos)
		ed:SetAngle(ang)	
			util.Effect("flame",ed)
			for i=0,self.Strength do
				local dir = (ang:Forward() + ((ang:Up() * (math.random() * 2 - 1)) + (ang:Right() * (math.random() * 2 - 1))) * 0.4):GetNormal()
				local pos = pos + dir * 4
				local flet = ents.Create("advturret_flamelet")
				flet:SetPos(pos)
				flet:SetOwner(self)
				flet:Spawn()
				flet:SetDamage(self.Damage)
				flet:SetBurnTime(self.BurnTime)
				local phy = flet:GetPhysicsObject()
				if phy:IsValid() then phy:ApplyForceCenter(dir * (12 + math.random() * (3*self.Strength))) end
			end
		
		self.NextAttack = CurTime() + self.FireDelay	 
	end
end

function ENT:RotateToTarget()
	timer.Create("AimAtTargetFIRE"..self:EntIndex(), 0.02, 0, function()
		
		if self:GetPos():Distance(self.Target:GetPos()) > self.Range or not self.Target:Alive() then 
			timer.Destroy("AimAtTargetFIRE"..self:EntIndex())
			self.Target = nil
			self.AttackCounter = 0
			return
		end
		
		local targetPos = self.Target:GetPos()
		local ang = (targetPos - self.Position):Angle()		
		
		ang.r, ang.p, ang.y = 0, 0, math.NormalizeAngle(ang.y)		
		self:SetAngles(ang)	
		
		self:AttackTarget()
	end)
end


function ENT:Think()	
	self:NextThink( CurTime() + 1 )	
	
	if not self.Target then
		for _, pl in pairs(player.GetAll()) do
			if self.Position:Distance(pl:GetPos()) < self.Range then --and self.TurretOwner != pl then
				self.Target = pl
				umsg.Start("targetFIRE")
					umsg.Entity(self.Target)
				umsg.End()
				self:RotateToTarget()
			end
		end		
		self:NextThink( CurTime() + 0.1 )
	end				
	
    return true
end

function ENT:OpenMenu()
	umsg.Start("OpenTurretInfo")
		umsg.String(self.ID)
		umsg.Short(self.Level)
	umsg.End()
end

function ENT:Use(activator, caller)
	if caller:IsPlayer() then
		self:OpenMenu()
	end
end


function ENT:Upgrade()
	local newLevel = self.Level + 1
	if not TURRETS.AllTurrets[self.TType..newLevel] then return false end
	
	self.Level = newLevel
	self.Reference = TURRETS.AllTurrets[self.TType..self.Level]
	self.Damage = self.Reference.Damage
	self.Range = self.Reference.Range
	self.FireDelay = self.Reference.FireDelay
	self:OpenMenu()
	return true
end